var vueApp = new Vue({
  el: '#app',
  data: function () {
    return {
    	  keyword:'',
    	  dictData:[],
    	  currentPageNbr: config.currentPageNbr,
          pageSizes: config.pageSizes,
          pageSize: config.defaultpageSize,
          totalRecords: 0,
          addUrl:'dictadd.html?',
          showDetail: false,
          expands:[],
          currentId:'',
          detailData:[]
      };
  },
  mounted: function () {
    this.initData();

  },
  methods: {
    initData:function(){
       var that = this;
//       userEx.getUserInfo(function(data){
//            that.user_ssbm = data.userinfo.deptCode;       
//            tools.checkPriv(data.userinfo.privNames,"cbgs");         
//       });
//       tools.checkPriv(userExInfo.privNames,"cbgs"); 
       this.onSearch();
       
    },
    getRowKeys:function(row) {
        return row.dict_id;
    },
    onSearch:function(){
    	var that = this;
        var jso = {
      	   "keyword": this.keyword
      	};
        var postdata = {
      	    "jsonStr":JSON.stringify(jso),    		 
            "currentPageNbr" : this.currentPageNbr,
            "pageSize" : this.pageSize
        };
        
        tools.ajaxRequestAsyncEx("pub/userdict/udDetailList",postdata, function(data){
            if (data.respCode == "0") {
                that.dictData = data.mastlist;
                that.totalRecords = data.page.totalRecords;       
            } else {
                that.dictData = [];
                that.totalRecords =0;
                that.currentPageNbr=1;
                that.showError(data.respDesc);      
            }
        });
    	
    	
    },
    del:function(row,type){
    	var that= this;
    	this.$confirm('是否确认删除字典 '+row.code_desc+' ?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
       }).then(() => {
    	   	var jso = {
	       		"dict_id": row.dict_id
	       	};
	       	var reqData={
	       		"jsonStr":JSON.stringify(jso),    			
	   	    };
	       	tools.ajaxRequestAsyncEx("pub/userdict/udDel", reqData, function(data){
	   	   		if (data.respCode != 0) {
	   	   			that.showError(data.respDesc);    
	   	   			return false;
	   	   		}else{
	   	   			that.$message({
	   	                showClose: true,
	   	                type: 'success',
	   	                message: '删除成功!'
	   	            });  
	   	   			if(type==0){
	   	   				that.onSearch();
	   	   			}else{
	   	   				that.detailData.forEach(function(item,idx){
	   	   					if(item.dict_id==row.dict_id){
	   	   						that.detailData.splice(idx,1);
	   	   					}
	   	   				});
	   	   			}
	   	   			
	   	   		}    	   			
	   	    });
       	
       }).catch(() => {
          		return false;
       });
  	},  	
    
    newDict:function(){
    	var that = this;
    	var url= this.addUrl+"oper=add&v="+Math.random();
    	this.$refs.dcdg.open(url, {   
            fullscreen: true, 
            title: "字典添加", 
            callback: function(arg) {
              var item = JSON.parse(arg);
              if(item.msg!=undefined){
                that.onSearch();
              }
            }
    	}); 
    },
    
    detail:function(row){
    	var that = this;
    	this.expands = []; 
    	if(this.currentId!=row.dict_id){
    		this.expands.push(row.dict_id);  
    	}else{
    		this.currentId="";
    	}    	   
    	if(this.expands.length>0){
	    	this.showDetail = true;
	    	this.currentId = row.dict_id;
	    	this.$nextTick(() => {  
	    		that.getDetail();
	        });
    	}  	
    },
    getDetail:function(){
    	var that = this;
    	this.detailData = [];
    	this.dictData.forEach(function(item,idx){
    		if(item.dict_id==that.currentId){
    			that.detailData = item.detaillist;
    			console.log(that.detailData);
    		}
    	});    	    	
    	
    },
    edit:function(row){
    	var that =this;
    	var url = this.addUrl+"oper=edit&dictId="+row.dict_id+"&v="+Math.random();
    	this.$refs.dcdg.open(url, {   
            fullscreen: true, 
            title: "字典编辑", 
            callback: function(arg) {
              var item = JSON.parse(arg);
              if(item.msg!=undefined){
                that.onSearch();
              }
            }
    	}); 
    },    
    
    search:function(){
    	this.currentPageNbr="1";
        this.onSearch();
    },
    handleSizeChange:function(pageSize){
        this.pageSize = pageSize;
        this.currentPageNbr="1";
        this.onSearch();
    },

    handleCurrentChange:function(currentPageNbr){
      this.currentPageNbr = currentPageNbr;
      this.onSearch();
    },

    showError: function(info){
        var i = (info==undefined?"加载数据错误":info);
        this.$alert(i, '系统错误', {
             confirmButtonText: '确定'
        }); 
    },

  },
  watch:{
//      currentPageNbr : function() {
//          // alert(this.currentPageNbr);
//          this.onSearch();
//      },
//        
//      pageSize : function() {
//          // alert(this.currentPageNbr);
//    	  if(this.currentPageNbr=="1"){
//    		  this.onSearch();
//    	  }else{
//    		  this.currentPageNbr=="1";
//    	  }         
//      },
  }
})